package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryData;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组，操作t_checkgroup表
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多的关联关系，操作t_checkgroup_checkitem表
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    /*public void add(Integer[] che, CheckGroup checkGroup) {
        checkGroupDao.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(),che);
        System.out.println("1---"+Arrays.toString(che));
    }*/

    /*public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] che){
        System.out.println("2---"+Arrays.toString(che));
        if(che!=null && che.length>0){
            for (Integer checkitemId : che) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }

    }*/

    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            StringBuffer buf = new StringBuffer();

            for (Integer checkitemId : checkitemIds) {
                buf.append("(" + checkGroupId + "," + checkitemId + "),");
            }
            try {
                if (buf.length() > 0) {
                    String str = buf.substring(0, buf.length() - 1);
                    checkGroupDao.setCheckGroupAndCheckItem(str);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        QueryData queryData = queryPageBean.getQueryData();
        PageHelper.startPage(currentPage, pageSize);
        /*Map<String,String> map = new HashMap<>();
//        if(queryData.getQueryString_code()!=null&&queryData.getQueryString_name()!=null&&queryData.getQueryString_helpCode()!=null){
            map.put("queryString_code",queryData.getQueryString_code());
            map.put("queryString_name",queryData.getQueryString_name());
            map.put("queryString_helpCode",queryData.getQueryString_helpCode());*/
//        }
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryData);
        PageResult pr = new PageResult(page.getTotal(), page.getResult());
        return pr;
    }

    /**
     * 1、根据id查询检查组 1 需要查询检查组内容
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(int id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(int id) {
        List<Integer> list = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return list;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组，操作t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //设置检查组和检查项的多对多的关联关系，操作t_checkgroup_checkitem表
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.delete(checkGroupId);
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    /**
     * 批量删除套餐
     *
     * @param id
     */
    @Override
    public boolean delete(Integer[] id) {
        StringBuffer buf = new StringBuffer();
        for (Integer i : id) {
            buf.append(i+",");
        }
        if (buf.length() > 0) {
            String str =
                    buf.substring(0, buf.length() - 1);
            int a = checkGroupDao.selectSetmealByGroupId(str);
            if (a == 0) {
                checkGroupDao.deleteGroupItemByGroupIds(str);
                checkGroupDao.deletes(str);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }

}
